//
//  KLMBaseModalController.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/5.
//

import UIKit

enum ModalConfirmType {
    case addOrRenameRoom
//    case addOrRenameMode
//    case addOrRenameHome
//    case renameMemberNickName
    case add
    case rename
    case tipOnlyTitle
    case tipMessage
}

class KLMBaseModalController: KLMBaseController {

    typealias ConfirmBlock = (_ text: String?, _ type: ModalConfirmType) -> Void
    typealias CancelBlock = () -> ()
    
    private var type: ModalConfirmType
    private var operTitle: String?
    private var message: String?
    private var textFieldTip: String?
    private var cancelTitle: String?
    private var confirmTitle: String?
    
    var confirmBlock: ConfirmBlock?
    var cancelBlock: CancelBlock?
    
    @IBOutlet weak var containerView: KLMView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var editTextField: CMTextField!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var confirmBtn: UIButton!
    @IBOutlet weak var messageLabel: UILabel!
    @IBOutlet weak var titleToLineCons: NSLayoutConstraint!//98\500
    @IBOutlet weak var titleToMessageCons: NSLayoutConstraint!//21\600
    @IBOutlet weak var titleToFieldCons: NSLayoutConstraint!//18\750
    @IBOutlet weak var vLine: UIView!
    @IBOutlet weak var hLine: UIView!
    
    init(type: ModalConfirmType = .addOrRenameRoom, operTitle: String? = nil, message: String? = nil, textFieldTip: String? = nil, cancelTitle: String? = nil, confirmTitle: String? = nil, cancelBlock: CancelBlock? = nil, confirmBlock: ConfirmBlock? = nil) {
        self.type = type
        self.operTitle = operTitle
        self.message = message
        self.textFieldTip = textFieldTip
        self.cancelTitle = cancelTitle
        self.confirmTitle = confirmTitle
        self.cancelBlock = cancelBlock
        self.confirmBlock = confirmBlock
        super.init(nibName: "KLMBaseModalController", bundle: nil)
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = rgba(0, 0, 0, 0.3)
        switch type {
        case .tipMessage:
            titleToLineCons.priority = UILayoutPriority(250.0)
            titleToMessageCons.priority = UILayoutPriority(750.0)
            titleToFieldCons.priority = UILayoutPriority(250.0)
            titleToLineCons.constant = 0
            titleToMessageCons.constant = 25
            titleToFieldCons.constant = 0
            editTextField.isHidden = true
            messageLabel.isHidden = false
        case .tipOnlyTitle:
            titleToLineCons.priority = UILayoutPriority(750.0)
            titleToMessageCons.priority = UILayoutPriority(250.0)
            titleToFieldCons.priority = UILayoutPriority(250.0)
            titleToLineCons.constant = 21
            titleToMessageCons.constant = 0
            titleToFieldCons.constant = 0
            messageLabel.isHidden = true
            editTextField.isHidden = true
        default:
            titleToLineCons.priority = UILayoutPriority(250.0)
            titleToMessageCons.priority = UILayoutPriority(250.0)
            titleToFieldCons.priority = UILayoutPriority(750.0)
            titleToLineCons.constant = 0
            titleToMessageCons.constant = 0
            titleToFieldCons.constant = 18
            messageLabel.isHidden = true
            editTextField.isHidden = false
            editTextField.becomeFirstResponder()
        }
    }

    override func viewDidLoadConfig() {
        super.viewDidLoadConfig()
        hLine.backgroundColor = rgba(242, 242, 242, 1)
        vLine.backgroundColor = rgba(242, 242, 242, 1)
        
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: editTextField.height))
        editTextField.leftView  = leftView
        editTextField.leftViewMode = .always
        editTextField.layer.cornerRadius = 4
        
        titleLabel.configStyle(with: operTitle, font: UIFont(safeName: "Alata-Regular", size: 18), titleColor: rgba(0, 0, 0, 1))
        messageLabel.configStyle(with: message, font: UIFont(safeName: "PingFangSC-Regular", size: 14), titleColor: rgba(51, 51, 51, 1))
        editTextField.placeholder = textFieldTip
        
        cancelBtn.configStyle(font: UIFont(safeName: "Alata-Regular", size: 18), titleColor: rgba(0, 0, 0, 1))
        if let cancel = cancelTitle {
            cancelBtn.setTitle(cancel, for: .normal)
        } else {
            cancelBtn.setTitle(KLMLocalizable.LANGLOC("Cancel"), for: .normal)
        }
        confirmBtn.configStyle(font: UIFont(safeName: "Alata-Regular", size: 18), titleColor: rgba(0, 0, 0, 1))
        if let confirm = confirmTitle {
            confirmBtn.setTitle(confirm, for: .normal)
        } else {
            confirmBtn.setTitle(KLMLocalizable.LANGLOC("Confirm"), for: .normal)
        }
    }
    override func applyTheme(_ theme: any Theme) {
        super.applyTheme(theme)
        confirmBtn.configStyle(titleColor: theme.btnBgColor)
    }
    @IBAction func tapCancelBtn(_ sender: UIButton) {
        if let cancelBlock = cancelBlock {
            cancelBlock()
        }
        dismiss(animated: true, completion: nil)
    }
    
    @IBAction func tapConfirmBtn(_ sender: UIButton) {
        editTextField.resignFirstResponder()
        
        if type != .tipMessage && type != .tipOnlyTitle {
            guard let confirmBlock = confirmBlock, let text = editTextField.text, !text.isEmpty else {
                SVProgressHUD.showInfo(withStatus: "输入不能为空！")
                return
            }
            if type == .addOrRenameRoom, text.count > 20 {
                SVProgressHUD.showInfo(withStatus: "房间名称20 字以内！")
                return
            } else {
                dismiss(animated: true, completion: nil)
                confirmBlock(text, type)
            }
        } else {
            if let block = confirmBlock {
                dismiss(animated: true, completion: nil)
                block(nil, type)
            }
        }
    }
    
}
